package dog.gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

import constants.TYPE;
import containers.Message;
import dog.gui.appointmentView.IndexedJPanel;
import dog.model.Model;

@SuppressWarnings("serial")
public class MessagePanel
	{
	private NewsPanel newsPanel;
	private Model model;
	private String text;
	private Message message;
	public MessagePanel(final Message message, Model model,NewsPanel np)
		{
		super();
		this.newsPanel=np;
		this.model=model;
		this.message=message;
		//this.setPreferredSize(new Dimension(192,192));
		final int type = message.type;
		
		if (type==TYPE.INVITATION)
			setText("Invitation to meeting from " + model.getPerson(message.ownerPersonID).email+".");
		else if (type==TYPE.CHANGED)
			setText("An appointment has been changed.");
		else if (type==TYPE.DENIED)
			setText(model.getPerson(message.auxField).email + " has denied an invitation.");
		else if (type==TYPE.LEFT)
			setText(model.getPerson(message.auxField).email + " is no longer participating.");
		else if (type==TYPE.CANCELLED)
			setText("An appointment is canceled.");
		else if (type==TYPE.DELETED)
			setText("You are deleted from an Appointment.");
		}
	public JPanel getComponent()
		{
		JPanel panel = new IndexedJPanel(this.message.messageID);
		JTextArea area = new JTextArea(4,16);
		area.setText(text);
		area.setLineWrap(true);
		area.setWrapStyleWord(true);
		panel.add(area);
		area.addMouseListener(new MouseAdapter(){
			public void mouseReleased(MouseEvent e){
				{
				click();
				}
			}});
		return panel;
		}
	public void click()
		{
		if(model.getMessageContext()==null)
			{
			newsPanel.removeMessage(this);
			int appID=message.appointmentID;
			if (message.type==TYPE.CHANGED)
				appID=message.auxField;//ChangedTo
			model.gui_getAppointmentInfo(message.ownerPersonID, appID,message);
			}
		else
			System.out.println("plz wait");
		}

	private void setText(String message)
		{
		text=message;
		}

	public Message getMessage()
		{
		return message;
		}
	}
